package com.wson.controller.compass;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wson.controller.model.AbstractModelExecutionController;
import com.wson.model.basic.IBasicManip;
import com.wson.model.basic.IBasicView;
import com.wson.model.clouddisk.account.ICodiskAccountMeta;
import com.wson.model.clouddisk.file.ICodiskFileManip;
import com.wson.model.clouddisk.file.ICodiskFileMeta;
import com.wson.model.clouddisk.file.ICodiskFileView;
import com.wson.model.compass.status.ICompassStatusMeta;
import com.wson.platform.factory.impl.BeanFactory;
import com.wson.root.util.LoggerUtil;

public class InitialFolderController extends AbstractModelExecutionController {

	@Override
	protected Object handleRequestExecution(String action,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Object obj = super.handleRequestExecution(action, request, response);
		
		String folder = this.retrieveRequestParamString(request, ICompassStatusMeta.SYNC_INITIALFOLDER);
		Long accountID = this.retrieveRequestParamLong(request, ICodiskAccountMeta.SYNC_SELF+ICodiskAccountMeta.SYNC_ID);
		
		ICodiskFileManip file = (ICodiskFileManip)BeanFactory.getCurrentFactory().getModelFactory().getModel(ICodiskFileMeta.META_ID);
		this.initModel(request, file);
		file.setID(0L);
		file.setName(folder);
		file.setPath(folder);//BeanFactory.getCurrentFactory().getAppStub().getApplicationSyntax()+"//"+folder);
		file.setDirectory(true);
		file.setAccountID(accountID);
		
		file.selfCreate();
		
		LoggerUtil.DEBUG("code "+((ICodiskFileView)file).getCode());

		response.sendRedirect(((ICodiskFileView)file).selfProvision());
		//response.sendRedirect("https://www.box.com/files/temp#/files/0/f/495605175/temp");
		return obj;
	}

	@Override
	protected void handleRequestExecution(String action, IBasicView model,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		((IBasicManip)model).selfExecute(action);
		//super.handleRequestExecution(action, model, request, response);
	}

}
